<template>
  <div class="nav-bar">
    <div class="nav-container">
      <div class="logo-section">
        <div class="logo" @click="navigateTo('/')">
          <span class="logo-text">EduCoder</span>
          <span class="logo-subtitle">资料来自网友投稿</span>
        </div>
      </div>
      
      <div class="nav-links">
        <router-link 
          v-for="(item, index) in navItems" 
          :key="index"
          :to="item.path"
          class="nav-item"
          active-class="nav-active"
        >
          <el-icon class="nav-icon"><component :is="item.icon" /></el-icon>
          <span>{{ item.title }}</span>
        </router-link>
      </div>
      
      <div class="nav-actions">
        <slot name="actions"></slot>
      </div>
    </div>
    
    <!-- 移动端菜单按钮 -->
    <div class="mobile-menu-button" @click="toggleMobileMenu">
      <el-icon v-if="!mobileMenuOpen"><Menu /></el-icon>
      <el-icon v-else><Close /></el-icon>
    </div>
    
    <!-- 移动端菜单 -->
    <transition name="slide">
      <div class="mobile-menu" v-if="mobileMenuOpen">
        <div class="mobile-menu-header">
          <span class="logo-text">EduCoder</span>
          <el-icon class="close-icon" @click="closeMobileMenu"><Close /></el-icon>
        </div>
        
        <div class="mobile-menu-items">
          <router-link 
            v-for="(item, index) in navItems" 
            :key="index"
            :to="item.path"
            class="mobile-nav-item"
            active-class="mobile-nav-active"
            @click="closeMobileMenu"
          >
            <el-icon class="nav-icon"><component :is="item.icon" /></el-icon>
            <span>{{ item.title }}</span>
          </router-link>
        </div>
        
        <div class="mobile-actions">
          <slot name="mobile-actions"></slot>
        </div>
        
        <!-- 移动端赞赏和收录按钮 -->
        <div class="mobile-donate-buttons">
          <div class="mobile-donate-button" @click="showCollectQRCode">
            <div class="mobile-button-item">
              <el-icon><List /></el-icon>
              <span>收录</span>
            </div>
          </div>
          
          <div class="mobile-donate-button" @click="showCoffeeQRCode">
            <div class="mobile-button-item">
              <el-icon><Coffee /></el-icon>
              <span>Coffee</span>
            </div>
          </div>
        </div>
      </div>
    </transition>
    
    <!-- 移动端二维码弹窗 -->
    <el-dialog
      v-model="qrDialogVisible"
      :title="qrDialogTitle"
      width="90%"
      center
      class="qr-dialog"
    >
      <div class="qr-container">
        <img 
          :src="qrImageUrl" 
          :alt="qrDialogTitle" 
          class="qr-image"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { 
  Menu, 
  Close,
  List,
  Coffee
} from '@element-plus/icons-vue';

const props = defineProps({
  navItems: {
    type: Array,
    default: () => [
      { title: '题库预览', path: '/preview', icon: 'Collection' },
      { title: '文件阅读器', path: '/file-reader', icon: 'Document' },
    ]
  }
});

const router = useRouter();
const mobileMenuOpen = ref(false);
const qrDialogVisible = ref(false);
const qrDialogTitle = ref('');
const qrImageUrl = ref('');

const navigateTo = (path) => {
  router.push(path);
  closeMobileMenu();
};

const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value;
  
  // 当菜单打开时，禁止页面滚动
  if (mobileMenuOpen.value) {
    document.body.style.overflow = 'hidden';
  } else {
    document.body.style.overflow = '';
  }
};

const closeMobileMenu = () => {
  mobileMenuOpen.value = false;
  document.body.style.overflow = '';
};

// 显示收录二维码
const showCollectQRCode = () => {
  qrDialogTitle.value = '收录题库';
  qrImageUrl.value = 'https://mybox-1257251314.cos.ap-chengdu.myqcloud.com/upload/myself/wechat.jpg';
  qrDialogVisible.value = true;
  closeMobileMenu();
};

// 显示Coffee二维码
const showCoffeeQRCode = () => {
  qrDialogTitle.value = '请我喝奶茶';
  qrImageUrl.value = 'https://mybox-1257251314.cos.ap-chengdu.myqcloud.com/upload/myself/payforme.jpg';
  qrDialogVisible.value = true;
  closeMobileMenu();
};
</script>

<style scoped>
.nav-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 0.75rem 1rem;
}

.nav-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.logo-section {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.logo-text {
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #3b82f6, #f97316);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.logo-subtitle {
  font-size: 0.85rem;
  color: #6b7280;
  display: inline-block;
}

.logo-badge {
  padding: 0.15rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 500;
  border-radius: 9999px;
  color: white;
  background: linear-gradient(135deg, #3b82f6, #f97316);
}

.nav-links {
  display: flex;
  gap: 0.5rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  color: #4b5563;
  text-decoration: none;
  transition: all 0.2s;
}

.nav-item:hover {
  background-color: #f3f4f6;
  color: #3b82f6;
}

.nav-active {
  background-color: #eff6ff;
  color: #3b82f6;
  font-weight: 500;
}

.nav-icon {
  font-size: 1.25rem;
}

.nav-actions {
  display: flex;
  gap: 0.75rem;
}

/* 移动端菜单 */
.mobile-menu-button {
  display: none;
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 101;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #f97316);
  color: white;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.mobile-menu {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: white;
  z-index: 100;
  padding: 1rem 1rem 5rem;
  overflow-y: auto;
  flex-direction: column;
}

.mobile-menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 0;
  margin-bottom: 1rem;
  border-bottom: 1px solid #f3f4f6;
}

.close-icon {
  font-size: 1.5rem;
  cursor: pointer;
  color: #6b7280;
}

.mobile-menu-items {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.mobile-nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: 0.375rem;
  color: #4b5563;
  text-decoration: none;
  font-size: 1.125rem;
}

.mobile-nav-active {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(249, 115, 22, 0.1));
  color: #3b82f6;
  font-weight: 500;
  border-left: 3px solid #3b82f6;
}

.mobile-actions {
  margin-top: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* 移动端赞赏和收录按钮 */
.mobile-donate-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid #f3f4f6;
}

.mobile-donate-button {
  width: 48%;
}

.mobile-button-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem;
  border-radius: 0.5rem;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(249, 115, 22, 0.1));
  cursor: pointer;
  transition: all 0.3s ease;
}

.mobile-button-item:hover {
  transform: translateY(-2px);
}

.mobile-button-item .el-icon {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, #3b82f6, #f97316);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.mobile-button-item span {
  font-size: 0.85rem;
  font-weight: 500;
  color: #4b5563;
}

/* 移动端二维码弹窗 */
.qr-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1rem;
}

.qr-image {
  max-width: 100%;
  height: auto;
  max-height: 300px;
  border-radius: 8px;
}

:deep(.qr-dialog .el-dialog__header) {
  padding: 1rem;
  margin: 0;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.05) 0%, rgba(249, 115, 22, 0.05) 100%);
  border-bottom: 1px solid #f3f4f6;
}

:deep(.qr-dialog .el-dialog__title) {
  font-weight: 600;
  background: linear-gradient(135deg, #3b82f6, #f97316);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* 移动端菜单动画 */
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.3s ease;
}

.slide-enter-from,
.slide-leave-to {
  transform: translateX(100%);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-links, 
  .nav-actions {
    display: none;
  }
  
  .mobile-menu-button,
  .mobile-menu {
    display: flex;
  }
  
  .nav-bar {
    padding: 0.75rem;
  }
  
  .logo-subtitle {
    display: none;
  }
  
  /* 确保弹出层在移动端正确显示 */
  :deep(.el-popper) {
    max-width: 90vw !important;
  }
  
  :deep(.donate-popover) {
    width: 90vw !important;
    max-width: 280px !important;
  }
  
  :deep(.donate-image) {
    width: 100% !important;
    height: auto !important;
    max-width: 240px;
  }
  
  /* 修复移动端按钮样式 */
  :deep(.el-button) {
    font-size: 0.9rem !important;
    padding: 8px 15px !important;
  }
}
</style> 